package com.yinhe.module.cjl;

import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.core.domain.entity.SysRole;
import com.yinhe.common.core.domain.entity.SysUser;
import com.yinhe.common.core.domain.model.LoginUser;
import com.yinhe.common.core.page.TableDataInfo;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.exception.ServiceException;
import com.yinhe.common.utils.poi.ExcelUtil;

import com.yinhe.system.domain.YhLeave;

import com.yinhe.system.msg.mssage.domain.YhMsgInner;
import com.yinhe.system.msg.ws.service.WebSocketService;
import com.yinhe.system.service.ISysDeptService;
import com.yinhe.system.service.IYhLeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 休假记录Controller
 *
 * @author yinhe
 * @date 2022-07-26
 */
@RestController
@RequestMapping("/module/cjl/leave")
public class CjlLeaveController extends BaseController {
    @Autowired
    private IYhLeaveService yhLeaveService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    public WebSocketService webSocketService;


    /**
     * 查询休假记录列表
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:leave:list')")
    @GetMapping("/list")

    public TableDataInfo list(YhLeave yhLeave) {
        startPage();
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        List<YhLeave> list = yhLeaveService.selectYhLeaveList(yhLeave);
        //获取当前userid
        getUserId();
        //查询rolekey
        SysRole keyList = yhLeaveService.selectRolekey(getUserId());
        String key = keyList.getRoleKey().substring(0, 4);
        if (key.equals("1001") || key.equals("1002")) {
            startPage();
            List<YhLeave> chairlist = yhLeaveService.selectYhList(yhLeave);
            return getDataTable(chairlist);
        } else if (key.equals("2001")) {
            startPage();
            yhLeave.setLeaveTotalState("1");
            List<YhLeave> deptlist = yhLeaveService.selectYhLeaveList(yhLeave);
            return getDataTable(deptlist);
        } else if (key.equals("3001")) {
            startPage();
            List<YhLeave> colist = yhLeaveService.selectYhLeaveList(yhLeave);
            return getDataTable(colist);
        }

        return getDataTable(list);

    }

    /**
     * 导出休假记录列表
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:leave:export')")
    @Log(title = "休假记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhLeave yhLeave) {
        List<YhLeave> list = yhLeaveService.selectYhLeaveList(yhLeave);
        ExcelUtil<YhLeave> util = new ExcelUtil<YhLeave>(YhLeave.class);
        util.exportExcel(response, list, "请假记录数据");
    }

    /**
     * 获取休假记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:leave:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(yhLeaveService.selectYhLeaveById(id));
    }

    /**
     * 新增休假记录
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:leave:add')")
    @Log(title = "休假记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhLeave yhLeave) {

        addBaseInfo(yhLeave);
        return toAjax(yhLeaveService.insertYhLeave(yhLeave));
    }
    // 插入基本信息
    private void addBaseInfo(YhLeave yhLeave) {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        // 1.组合消息体
        YhMsgInner msgInner = new YhMsgInner();
        msgInner.setMsgTitle("请假申请");
        msgInner.setMsgContent("已申请请假");
        msgInner.setSendUserName(sysUser.getUserName());
        msgInner.setNote1(sysUser.getNickName());
        msgInner.setNote2("/zonghe/yygleave");
        //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
        List<SysUser> result = yhLeaveService.selectUserByDeptId(sysUser.getDeptId());
        for (SysUser a:result){
            a.getUserName();
            request.getServletContext().setAttribute("userName", a.getUserName());
        }
        String userName=(String) request.getServletContext().getAttribute("userName");
        // 2.组合接收者列表
        List<String> receiveNames = new ArrayList<>();
        // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
        receiveNames.add(userName);
        // 3.调用服务
        webSocketService.PushMsg(msgInner, receiveNames);
        // 存入userId和deptId，做数据权限
        yhLeave.setUserId(sysUser.getUserId());
        yhLeave.setDeptId(sysUser.getDeptId());
        yhLeave.setUsername(sysUser.getUserName());
        yhLeave.setIdNumber(sysUser.getIdNumber());
        yhLeave.setLeaveTotalState("1");//已提交，待部门审核
        yhLeave.setLeaveSituation("1");//未销假
        yhLeave.setNote1("0");//监控局长政委是否已审核
        yhLeave.setNote2("0");//监控是否已销假
        yhLeave.setDepartmentHeadIsacting("1");
        yhLeave.setDirectorGeneralIsacting("1");
        yhLeave.setPoliticalCommissarIsacting("1");
        yhLeave.setCreateBy(getUsername());
        yhLeave.setCreateTime(new Date());
    }

    /**
     * 修改休假记录
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:leave:edit')")
    @Log(title = "休假记录", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody YhLeave yhLeave) {
        if (yhLeave.getLeaveTotalState().equals("1") || yhLeave.getLeaveTotalState().equals("2")|| yhLeave.getLeaveTotalState().equals("3")) {
            throw new ServiceException("正在审核中,请耐心等待！");
        }
        if (yhLeave.getLeaveTotalState().equals("4")) {
            throw new ServiceException("已审核结束！");
        }
        if (yhLeave.getLeaveTotalState().equals("0")) {
            yhLeave.setLeaveTotalState("1");//待部门审核
            yhLeave.setDepartmentHeadResult("  ");
            yhLeave.setDirectorGeneralResult("  ");
            yhLeave.setPoliticalCommissarResult("  ");
            yhLeave.setDepartmentHeadSuggest("");//部门审核意见
            yhLeave.setDirectorGeneralSuggest("");//局长审核意见
            yhLeave.setPoliticalCommissarSuggest("");//政委审核意见
        }
        yhLeave.setNote1("0");//监控局长政委是否已审核
        yhLeave.setUpdateBy(getUsername());
        yhLeave.setUpdateTime(new Date());
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        // 1.组合消息体
        YhMsgInner msgInner = new YhMsgInner();
        msgInner.setMsgTitle("请假申请");
        msgInner.setMsgContent("已申请请假");
        msgInner.setSendUserName(sysUser.getUserName());
        msgInner.setNote1(sysUser.getNickName());
        msgInner.setNote2("/zonghe/yygleave");
        //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
        String username=(String) request.getServletContext().getAttribute("username");
        // 2.组合接收者列表
        List<String> receiveNames = new ArrayList<>();
        // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
        //receiveNames.add(sysUser.getUserName());
        receiveNames.add(username);
        // 3.调用服务
        webSocketService.PushMsg(msgInner, receiveNames);
        return toAjax(yhLeaveService.updateYhLeave(yhLeave));
    }

    /**
     * 销假记录
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:leave:back')")
    @Log(title = "休假记录", businessType = BusinessType.UPDATE)
    @PutMapping("/back")
    public AjaxResult back(@RequestBody YhLeave yhLeave) {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        yhLeave.setUpdateBy(getUsername());
        yhLeave.setUpdateTime(new Date());
        if(yhLeave.getNote2().equals("1"))
        {
            throw new ServiceException("您已销假，请勿重复提交！");
        }
        yhLeave.setNote2("1");//已销假
        if (yhLeave.getLeaveTotalState().equals("0") ) {
            throw new ServiceException("审核通过后，才能销假！");
        }
        if (yhLeave.getLeaveTotalState().equals("1") || yhLeave.getLeaveTotalState().equals("2")|| yhLeave.getLeaveTotalState().equals("3")) {
            throw new ServiceException("正在审核中,请耐心等待！");
        }
        if(yhLeave.getLeaveTotalState().equals("4"))
        {
            yhLeave.setLeaveSituation("2");
        }
        return toAjax(yhLeaveService.backYhLeave(yhLeave));
    }

    /**
     * 部门审核休假记录
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:leave:view')")
    @Log(title = "休假记录", businessType = BusinessType.UPDATE)
    @PutMapping("/view")
    public AjaxResult view(@RequestBody YhLeave yhLeave) {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        updateBaseInfo(yhLeave);
        yhLeave.setUpdateBy(getUsername());
        yhLeave.setUpdateTime(new Date());
        yhLeave.setDepartmentHeadName(getUsername());
        yhLeave.setDepartmentHeadIdnum(sysUser.getIdNumber());
        return toAjax(yhLeaveService.viewYhLeave(yhLeave));
    }

   // 插入基本信息
    private void updateBaseInfo(YhLeave yhLeave) {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        String ph=(String) request.getServletContext().getAttribute("phone");
        SysUser sysUserDi =  yhLeaveService.selectDirector();
        SysUser sysUserCo =  yhLeaveService.selectCommissor();
        long phone = Long.parseLong(ph);
        System.out.println(phone+"000");
        String auto="0123456789";
        long late = getLate(phone, auto);
        String audit = String.valueOf(late);
        String baudit=audit.substring(0,6);
        System.out.println(baudit +"11");
        if(yhLeave.getDepartmentHeadIsacting().equals("1")) {
            if (yhLeave.getDepartmentHeadResult().equals("2")) {
                yhLeave.setLeaveTotalState("0");//审核不通过
                // 1.组合消息体
                YhMsgInner msgInner = new YhMsgInner();
                msgInner.setMsgTitle("请假申请");
                msgInner.setMsgContent("审核不通过");
                msgInner.setSendUserName(sysUser.getUserName());
                msgInner.setNote1(sysUser.getNickName());
                msgInner.setNote2("/zonghe/yygleave");
                //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
                // 2.组合接收者列表
                List<String> receiveNames = new ArrayList<>();
                // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
                //receiveNames.add(sysUser.getUserName());
                receiveNames.add(yhLeave.getUsername());
                // 3.调用服务
                webSocketService.PushMsg(msgInner, receiveNames);
            } else if (yhLeave.getDepartmentHeadResult().equals("1")) {
                yhLeave.setLeaveTotalState("2");//待局长政委审核
                // 1.组合消息体
                YhMsgInner msgInner = new YhMsgInner();
                msgInner.setMsgTitle("请假申请");
                msgInner.setMsgContent("部门审核通过");
                msgInner.setSendUserName(sysUser.getUserName());
                msgInner.setNote1(sysUser.getNickName());
                msgInner.setNote2("/zonghe/yygleave");
                //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
                // 2.组合接收者列表
                List<String> receiveNames = new ArrayList<>();
                // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
                //receiveNames.add(sysUser.getUserName());
                receiveNames.add(sysUserDi.getUserName());
                receiveNames.add(sysUserCo.getUserName());
                // 3.调用服务
                webSocketService.PushMsg(msgInner, receiveNames);
            }
        }else if(yhLeave.getDepartmentHeadCharacter().equals(baudit)&&yhLeave.getDepartmentHeadIsacting().equals("2")){
            if (yhLeave.getDepartmentHeadResult().equals("2")) {
                yhLeave.setLeaveTotalState("0");//审核不通过
                // 1.组合消息体
                YhMsgInner msgInner = new YhMsgInner();
                msgInner.setMsgTitle("请假申请");
                msgInner.setMsgContent("审核不通过");
                String username=(String) request.getServletContext().getAttribute("username");
                String nickname=(String) request.getServletContext().getAttribute("nickname");
                msgInner.setSendUserName(username);
                msgInner.setNote1(nickname);
                msgInner.setNote2("/zonghe/yygleave");
                //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
                // 2.组合接收者列表
                List<String> receiveNames = new ArrayList<>();
                // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
                //receiveNames.add(sysUser.getUserName());
                receiveNames.add(yhLeave.getUsername());
                // 3.调用服务
                webSocketService.PushMsg(msgInner, receiveNames);
            } else if (yhLeave.getDepartmentHeadResult().equals("1")) {
                yhLeave.setLeaveTotalState("2");//待局长政委审核
                // 1.组合消息体
                YhMsgInner msgInner = new YhMsgInner();
                msgInner.setMsgTitle("请假申请");
                msgInner.setMsgContent("部门审核通过");
                String username=(String) request.getServletContext().getAttribute("username");
                String nickname=(String) request.getServletContext().getAttribute("nickname");
                msgInner.setSendUserName(username);
                msgInner.setNote1(nickname);
                msgInner.setNote2("/zonghe/yygleave");
                //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
                // 2.组合接收者列表
                List<String> receiveNames = new ArrayList<>();
                // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
                //receiveNames.add(sysUser.getUserName());
                receiveNames.add(sysUserDi.getUserName());
                receiveNames.add(sysUserCo.getUserName());
                // 3.调用服务
                webSocketService.PushMsg(msgInner, receiveNames);
            }
        }else {
            throw new ServiceException("标识验证失败，请重新确认！");
        }
    }

    /**
     * 局长审核休假记录
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:leave:chief')")
    @Log(title = "休假记录", businessType = BusinessType.UPDATE)
    @PutMapping("/chief")
    public AjaxResult chief(@RequestBody YhLeave yhLeave) {
        if(yhLeave.getLeaveTotalState().equals("4"))
        {
            throw new ServiceException("已审核通过！");
        }
        if(yhLeave.getNote1().equals("1"))
        {
            throw new ServiceException("您已审核！");
        }
        yhLeave.setNote1("1");//局长已审核
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        chiefBaseInfo(yhLeave);
        yhLeave.setUpdateBy(getUsername());
        yhLeave.setUpdateTime(new Date());
        yhLeave.setDirectorGeneralName(getUsername());
        yhLeave.setDirectorGeneralIdnum(sysUser.getIdNumber());
        return toAjax(yhLeaveService.chiefYhLeave(yhLeave));
    }

    // 插入基本信息
    private void chiefBaseInfo(YhLeave yhLeave) {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        SysUser sysUserDi =  yhLeaveService.selectDirector();
        SysUser sysUserCo =  yhLeaveService.selectCommissor();
        String ph=(String) request.getServletContext().getAttribute("Dphone");
        long phone = Long.parseLong(ph);
        System.out.println(phone+"000");
        String auto="0123456789";
        long late = getLate(phone, auto);
        String audit = String.valueOf(late);
        String baudit=audit.substring(0,6);
        System.out.println(baudit +"11");
       if(yhLeave.getDirectorGeneralIsacting().equals("1")){
            if (yhLeave.getDirectorGeneralResult().equals("2")) {
                yhLeave.setLeaveTotalState("0");//审核不通过
                // 1.组合消息体
                YhMsgInner msgInner = new YhMsgInner();
                msgInner.setMsgTitle("请假申请");
                msgInner.setMsgContent("审核不通过");
                msgInner.setSendUserName(sysUser.getUserName());
                msgInner.setNote1(sysUser.getNickName());
                msgInner.setNote2("/zonghe/yygleave");
                //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
                // 2.组合接收者列表
                List<String> receiveNames = new ArrayList<>();
                // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
                //receiveNames.add(sysUser.getUserName());
                receiveNames.add(yhLeave.getUsername());
                // 3.调用服务
                webSocketService.PushMsg(msgInner, receiveNames);
            }
            if (Objects.isNull(yhLeave.getPoliticalCommissarResult())) {
                if (yhLeave.getDirectorGeneralResult().equals("1")) {
                    yhLeave.setLeaveTotalState("3");//待局长政委审核
                    // 1.组合消息体
                    YhMsgInner msgInner = new YhMsgInner();
                    msgInner.setMsgTitle("请假申请");
                    msgInner.setMsgContent("局长审核通过");
                    msgInner.setSendUserName(sysUser.getUserName());
                    msgInner.setNote1(sysUser.getNickName());
                    msgInner.setNote2("/zonghe/yygleave");
                    //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
                    // 2.组合接收者列表
                    List<String> receiveNames = new ArrayList<>();
                    // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
                    //receiveNames.add(sysUser.getUserName());
                    receiveNames.add(sysUserCo.getUserName());
                    // 3.调用服务
                    webSocketService.PushMsg(msgInner, receiveNames);
                }
            } else if (yhLeave.getPoliticalCommissarResult().equals("1") && yhLeave.getDirectorGeneralResult().equals("1")) {
                {
                    yhLeave.setLeaveTotalState("4");//已审核通过
                    // 1.组合消息体
                    YhMsgInner msgInner = new YhMsgInner();
                    msgInner.setMsgTitle("请假申请");
                    msgInner.setMsgContent("请假已审核通过");
                    msgInner.setSendUserName(sysUser.getUserName());
                    msgInner.setNote1(sysUser.getNickName());
                    msgInner.setNote2("/zonghe/yygleave");
                    //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
                    // 2.组合接收者列表
                    List<String> receiveNames = new ArrayList<>();
                    // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
                    //receiveNames.add(sysUser.getUserName());
                    receiveNames.add(yhLeave.getUsername());
                    // 3.调用服务
                    webSocketService.PushMsg(msgInner, receiveNames);

                }
            }
        }else if(yhLeave.getDirectorGeneralCharacter().equals(baudit)&&yhLeave.getDirectorGeneralIsacting().equals("2")){
            if (yhLeave.getDirectorGeneralResult().equals("2")) {
                yhLeave.setLeaveTotalState("0");//审核不通过
                // 1.组合消息体
                YhMsgInner msgInner = new YhMsgInner();
                msgInner.setMsgTitle("请假申请");
                msgInner.setMsgContent("审核不通过");
                msgInner.setSendUserName(sysUserDi.getUserName());
                msgInner.setNote1(sysUserDi.getNickName());
                msgInner.setNote2("/zonghe/yygleave");
                //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
                // 2.组合接收者列表
                List<String> receiveNames = new ArrayList<>();
                // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
                //receiveNames.add(sysUser.getUserName());
                receiveNames.add(yhLeave.getUsername());
                // 3.调用服务
                webSocketService.PushMsg(msgInner, receiveNames);
            }
            if (Objects.isNull(yhLeave.getPoliticalCommissarResult())) {
                if (yhLeave.getDirectorGeneralResult().equals("1")) {
                    yhLeave.setLeaveTotalState("3");//待局长政委审核
                    // 1.组合消息体
                    YhMsgInner msgInner = new YhMsgInner();
                    msgInner.setMsgTitle("请假申请");
                    msgInner.setMsgContent("局长审核通过");
                    msgInner.setSendUserName(sysUserDi.getUserName());
                    msgInner.setNote1(sysUserDi.getNickName());
                    msgInner.setNote2("/zonghe/yygleave");
                    //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
                    // 2.组合接收者列表
                    List<String> receiveNames = new ArrayList<>();
                    // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
                    //receiveNames.add(sysUserDi.getUserName());
                    receiveNames.add(sysUserCo.getUserName());
                    // 3.调用服务
                    webSocketService.PushMsg(msgInner, receiveNames);
                }
            } else if (yhLeave.getPoliticalCommissarResult().equals("1") && yhLeave.getDirectorGeneralResult().equals("1")) {
                {
                    yhLeave.setLeaveTotalState("4");//已审核通过
                    // 1.组合消息体
                    YhMsgInner msgInner = new YhMsgInner();
                    msgInner.setMsgTitle("请假申请");
                    msgInner.setMsgContent("请假已审核通过");
                    msgInner.setSendUserName(sysUserDi.getUserName());
                    msgInner.setNote1(sysUserDi.getNickName());
                    msgInner.setNote2("/zonghe/yygleave");
                    //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
                    // 2.组合接收者列表
                    List<String> receiveNames = new ArrayList<>();
                    // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
                    //receiveNames.add(sysUserDi.getUserName());
                    receiveNames.add(yhLeave.getUsername());
                    // 3.调用服务
                    webSocketService.PushMsg(msgInner, receiveNames);
                }
            }
        }else {
            throw new ServiceException("标识验证失败，请重新确认！");
        }

    }

    // 插入电话号码想与自编码，再与小时相乘
    private long getLate(long phone, String auto) {
        long autocode = Long.parseLong(auto);
        long re = phone & autocode;
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("kk");
        String dd = sdf.format(d);
        long time = Long.parseLong(dd);
        SimpleDateFormat sdf1 = new SimpleDateFormat("mm");
        String mm=sdf1.format(d);
        long m = Long.parseLong(mm);
        int a = 0;
        if(0<m && m<=15){
             a=1;
        }else if(15<m && m<=30){
             a=2;
        }else if(30<m && m<=45){
            a=3;
        }
        else if(45<m && m<60){
            a=4;
        }
        return re * time * a ;
    }

    /**
     * 政委审核休假记录
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:leave:gaott')")
    @Log(title = "休假记录", businessType = BusinessType.UPDATE)
    @PutMapping("/gaott")
    public AjaxResult gaott(@RequestBody YhLeave yhLeave) {
        if(yhLeave.getLeaveTotalState().equals("4"))
        {
            throw new ServiceException("已审核通过！");
        }
        if(yhLeave.getNote1().equals("2"))
        {
            throw new ServiceException("您已审核！");
        }
        yhLeave.setNote1("2");//政委已审核
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        gaottBaseInfo(yhLeave);
        yhLeave.setUpdateBy(getUsername());
        yhLeave.setUpdateTime(new Date());
        yhLeave.setPoliticalCommissarName(getUsername());
        yhLeave.setPoliticalCommissarIdnum(sysUser.getIdNumber());
        return toAjax(yhLeaveService.gaottYhLeave(yhLeave));
    }

    // 插入基本信息
    private void gaottBaseInfo(YhLeave yhLeave) {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        SysUser sysUserDi =  yhLeaveService.selectDirector();
        SysUser sysUserCo =  yhLeaveService.selectCommissor();
        String ph=(String) request.getServletContext().getAttribute("Cphone");
        long phone = Long.parseLong(ph);
        System.out.println(phone+"000");
        String auto="0123456789";
        long late = getLate(phone, auto);
        String audit = String.valueOf(late);
        String baudit=audit.substring(0,6);
        System.out.println(baudit +"11");
        if(yhLeave.getPoliticalCommissarIsacting().equals("1")) {
            if (yhLeave.getPoliticalCommissarResult().equals("2")) {
                yhLeave.setLeaveTotalState("0");//审核不通过
                // 1.组合消息体
                YhMsgInner msgInner = new YhMsgInner();
                msgInner.setMsgTitle("请假申请");
                msgInner.setMsgContent("审核不通过");
                msgInner.setSendUserName(sysUser.getUserName());
                msgInner.setNote1(sysUser.getNickName());
                msgInner.setNote2("/zonghe/yygleave");
                //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
                // 2.组合接收者列表
                List<String> receiveNames = new ArrayList<>();
                // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
                //receiveNames.add(sysUser.getUserName());
                receiveNames.add(yhLeave.getUsername());
                // 3.调用服务
                webSocketService.PushMsg(msgInner, receiveNames);
            }
            if (Objects.isNull(yhLeave.getDirectorGeneralResult())) {
                if (yhLeave.getPoliticalCommissarResult().equals("1")) {
                    yhLeave.setLeaveTotalState("3");//待局长政委审核
                    // 1.组合消息体
                    YhMsgInner msgInner = new YhMsgInner();
                    msgInner.setMsgTitle("请假申请");
                    msgInner.setMsgContent("政委审核通过");
                    msgInner.setSendUserName(sysUser.getUserName());
                    msgInner.setNote1(sysUser.getNickName());
                    msgInner.setNote2("/zonghe/yygleave");
                    //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
                    // 2.组合接收者列表
                    List<String> receiveNames = new ArrayList<>();
                    // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
                    //receiveNames.add(sysUser.getUserName());
                    receiveNames.add(sysUserDi.getUserName());
                    // 3.调用服务
                    webSocketService.PushMsg(msgInner, receiveNames);
                }
            } else if (yhLeave.getDirectorGeneralResult().equals("1")) {
                if (yhLeave.getPoliticalCommissarResult().equals("1")) {
                    yhLeave.setLeaveTotalState("4");//已审核通过
                    // 1.组合消息体
                    YhMsgInner msgInner = new YhMsgInner();
                    msgInner.setMsgTitle("请假申请");
                    msgInner.setMsgContent("请假已审核通过");
                    msgInner.setSendUserName(sysUser.getUserName());
                    msgInner.setNote1(sysUser.getNickName());
                    msgInner.setNote2("/zonghe/yygleave");
                    //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
                    // 2.组合接收者列表
                    List<String> receiveNames = new ArrayList<>();
                    // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
                    //receiveNames.add(sysUser.getUserName());
                    receiveNames.add(yhLeave.getUsername());
                    // 3.调用服务
                    webSocketService.PushMsg(msgInner, receiveNames);
                }
            }
        }else if(yhLeave.getPoliticalCommissarCharacter().equals(baudit)&&yhLeave.getPoliticalCommissarIsacting().equals("2"))
        {
            if (yhLeave.getPoliticalCommissarResult().equals("2")) {
                yhLeave.setLeaveTotalState("0");//审核不通过
                // 1.组合消息体
                YhMsgInner msgInner = new YhMsgInner();
                msgInner.setMsgTitle("请假申请");
                msgInner.setMsgContent("审核不通过");
                msgInner.setSendUserName(sysUserCo.getUserName());
                msgInner.setNote1(sysUserCo.getNickName());
                msgInner.setNote2("/zonghe/yygleave");
                //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
                // 2.组合接收者列表
                List<String> receiveNames = new ArrayList<>();
                // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
                //receiveNames.add(sysUser.getUserName());
                receiveNames.add(yhLeave.getUsername());
                // 3.调用服务
                webSocketService.PushMsg(msgInner, receiveNames);
            }
            if (Objects.isNull(yhLeave.getDirectorGeneralResult())) {
                if (yhLeave.getPoliticalCommissarResult().equals("1")) {
                    yhLeave.setLeaveTotalState("3");//待局长政委审核
                    // 1.组合消息体
                    YhMsgInner msgInner = new YhMsgInner();
                    msgInner.setMsgTitle("请假申请");
                    msgInner.setMsgContent("政委审核通过");
                    msgInner.setSendUserName(sysUserCo.getUserName());
                    msgInner.setNote1(sysUserCo.getNickName());
                    msgInner.setNote2("/zonghe/yygleave");
                    //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
                    // 2.组合接收者列表
                    List<String> receiveNames = new ArrayList<>();
                    // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
                    receiveNames.add(sysUserDi.getUserName());
                    //receiveNames.add(sysUserCo.getUserName());
                    // 3.调用服务
                    webSocketService.PushMsg(msgInner, receiveNames);

                }
            } else if (yhLeave.getDirectorGeneralResult().equals("1")) {
                if (yhLeave.getPoliticalCommissarResult().equals("1")) {
                    yhLeave.setLeaveTotalState("4");//已审核通过
                    // 1.组合消息体
                    YhMsgInner msgInner = new YhMsgInner();
                    msgInner.setMsgTitle("请假申请");
                    msgInner.setMsgContent("请假已审核通过");
                    msgInner.setSendUserName(sysUserCo.getUserName());
                    msgInner.setNote1(sysUserCo.getNickName());
                    msgInner.setNote2("/zonghe/yygleave");
                    //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
                    // 2.组合接收者列表
                    List<String> receiveNames = new ArrayList<>();
                    // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
                    //receiveNames.add(sysUserCo.getUserName());
                    receiveNames.add(yhLeave.getUsername());
                    // 3.调用服务
                    webSocketService.PushMsg(msgInner, receiveNames);
                }
            }
        }else
        {
            throw new ServiceException("标识验证失败，请重新确认！");
        }
    }

    /**
     * 删除休假记录
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:leave:remove')")
    @Log(title = "休假记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(yhLeaveService.deleteYhLeaveByIds(ids));
    }

    /**
     * 判断部门待审人员
     *
     */
    @RequestMapping("/searchAudit/{id}")
    public AjaxResult searchAduit(@PathVariable("id") Long id) {
        YhLeave yhLeave =  yhLeaveService.selectYhLeaveById(id);
        Long deptId = yhLeave.getDeptId();
        List<SysUser> result = yhLeaveService.selectUserByDeptId(deptId);
        for (SysUser a:result){
            a.getPhonenumber();
            a.getUserName();
            a.getNickName();
            request.getServletContext().setAttribute("username", a.getUserName());
            request.getServletContext().setAttribute("phone",a.getPhonenumber());
            request.getServletContext().setAttribute("nickname",a.getNickName());
        }
        return toAjax(1);
    }
    /**
     * 判断局长待审人员
     *
     */
    @RequestMapping("/director")
    public AjaxResult searchDirector() {
       SysUser sysUser =  yhLeaveService.selectDirector();
        request.getServletContext().setAttribute("Dusername",sysUser.getUserName());
        request.getServletContext().setAttribute("Dphone",sysUser.getPhonenumber());
        return  toAjax(1);
    }

    /**
     * 判断政委待审人员
     *
     */
    @RequestMapping("/commissor")
    public AjaxResult searchCommissor() {
        SysUser sysUser =  yhLeaveService.selectCommissor();
        request.getServletContext().setAttribute("Cphone",sysUser.getPhonenumber());
        return  toAjax(1);
    }
}


